package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-22
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入vodClient
    @Autowired
    private VodClient vodClient;



    //1 根据课程id删除小节
    //TODo 删除小节，删除对应视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();

        wrapper.eq("course_id",courseId);

        //根据courseID查询所有的视频
        QueryWrapper<EduVideo> wrappeerVideo = new QueryWrapper<>();
        wrappeerVideo.eq("course_id",courseId);
        //List<EduVideo> eduVideos = baseMapper.selectList(wrappeerVideo);//select *from eduVideo where course_Id =?;

        //查询指定的列
        wrappeerVideo.select("video_sourse_id");//select video_sourse_id from eduVideo where course_Id =?;
        List<EduVideo> eduVideoList = baseMapper.selectList(wrappeerVideo);

        //List<eduVideo>   变成List<String>
        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i < eduVideoList.size(); i++) {
            EduVideo eduVideo = eduVideoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            //放到videoIds集合里面
            videoIds.add(videoSourceId);

        }

        //多个视频id删除多个视频
        vodClient.deleteBatch(videoIds);
        baseMapper.delete(wrapper);//delete from eduvideo where courseId=?
    }
}
